package org.lyq.erp.process.goods.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.transaction.Transactional;
import org.lyq.erp.process.goods.mapper.TblWxGoodsMapper;
import org.lyq.erp.process.goods.pojo.AddGoodsVo;
import org.lyq.erp.process.goods.pojo.TblWxGoods;
import org.lyq.erp.process.goodsku.mapper.TblWxGoodSkuMapper;
import org.lyq.erp.process.goodsku.pojo.TblWxGoodSku;
import org.lyq.erp.utils.string.ObjectEmptyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TblWxGoodsServiceImpl implements TblWxGoodsService {

	@Autowired
	private TblWxGoodsMapper tblWxGoodsMapper;

	@Autowired
	private TblWxGoodSkuMapper tblWxGoodSkuMapper;

	@Override
	public List<TblWxGoods> selectAllGoods() {

		return tblWxGoodsMapper.selectAllGoods();
	}

	@Override
	public List<TblWxGoods> selectNotSaleList() {

		return tblWxGoodsMapper.selectNotSaleList();
	}

	@Override
	public List<TblWxGoods> selectNotCheckList() {

		return tblWxGoodsMapper.selectNotCheckList();
	}

	@Override
	@Transactional
	public void goodsSaleById(Integer goodsId) throws Exception {

		tblWxGoodsMapper.updateGoodsSaleById(goodsId);
	}

	@Override
	@Transactional
	public void goodsNotSaleById(Integer goodsId) throws Exception {

		tblWxGoodsMapper.updateGoodsNotSaleById(goodsId);
	}

	@Override
	@Transactional
	public void goodsCheckById(Integer goodsId) throws Exception {

		tblWxGoodsMapper.updateGoodsCheckById(goodsId);
	}

	@Override
	@Transactional
	public void goodsDeleteById(Integer goodsId) throws Exception {

		tblWxGoodsMapper.updateGoodsStatusById(goodsId);
	}

	@Override
	public AddGoodsVo selectGoodsInfo(AddGoodsVo addGoodsVo) {

		TblWxGoods tblWxGoods = tblWxGoodsMapper.selectByPrimaryKey(addGoodsVo.getGoodsId());
		if (tblWxGoods != null) {

			addGoodsVo.setGoodsId(tblWxGoods.getGoodsId());
			addGoodsVo.setGoodsCheck(tblWxGoods.getGoodsCheck());
			addGoodsVo.setGoodsDesc(getImgArr(tblWxGoods.getGoodsDesc()));
			addGoodsVo.setGoodsGallery(getImgArr(tblWxGoods.getGoodsGallery()));
			addGoodsVo.setGoodsHot(tblWxGoods.getGoodsHot());
			addGoodsVo.setGoodsImg(tblWxGoods.getGoodsImg());
			addGoodsVo.setGoodsWeight(tblWxGoods.getGoodsWeight());
			addGoodsVo.setGoodsUnit(tblWxGoods.getGoodsUnit());
			addGoodsVo.setGoodsTips(tblWxGoods.getGoodsTips());
			addGoodsVo.setGoodsOriginality(tblWxGoods.getGoodsOriginality());
			addGoodsVo.setGoodsNum(tblWxGoods.getGoodsNum());
			addGoodsVo.setGoodsName(tblWxGoods.getGoodsName());
			addGoodsVo.setGoodsIsfreight(tblWxGoods.getGoodsIsfreight());
			addGoodsVo.setGoodsIsrecom(tblWxGoods.getGoodsIsrecom());

			List<TblWxGoodSku> list = tblWxGoodSkuMapper.selectAllSkuByGoodId(addGoodsVo.getGoodsId());
			addGoodsVo.setGoodskuList(list);
			return addGoodsVo;
		}
		return null;
	}

	@Override
	@Transactional
	public void insertGoods(AddGoodsVo addGoodsVo) {

		List<TblWxGoodSku> goodskuList = addGoodsVo.getGoodskuList();
		TblWxGoods goods = new TblWxGoods();

		goods.setBrandId(null);
		goods.setCateId(null);
		goods.setGoodsCatidpath(null);
		goods.setGoodsCheck(addGoodsVo.getGoodsCheck());
		goods.setGoodsCreateTime(System.currentTimeMillis());
		goods.setGoodsDesc(getImgStr(addGoodsVo.getGoodsDesc()));
		goods.setGoodsFreightTempid(null);
		goods.setGoodsGallery(getImgStr(addGoodsVo.getGoodsGallery()));
		goods.setGoodsHot(addGoodsVo.getGoodsHot());
		goods.setGoodsImg(addGoodsVo.getGoodsImg());
		goods.setGoodsWeight(addGoodsVo.getGoodsWeight());
		goods.setGoodsWarnStock(10);
		goods.setGoodsUpdateTime(System.currentTimeMillis());
		goods.setGoodsUnit(addGoodsVo.getGoodsUnit());
		goods.setGoodsTips(addGoodsVo.getGoodsTips());
		goods.setGoodsStock(addGoodsVo.getGoodsStock());
		goods.setGoodsStatus(1);
		goods.setGoodsSn(null);
		goods.setGoodsSalenum(0);
		goods.setGoodsPv(0);
		goods.setGoodsOriginality(addGoodsVo.getGoodsOriginality());
		goods.setGoodsNum(addGoodsVo.getGoodsNum());
		goods.setGoodsName(addGoodsVo.getGoodsName());
		goods.setGoodsIsfreight(addGoodsVo.getGoodsIsfreight());
		goods.setGoodsIsrecom(addGoodsVo.getGoodsIsrecom());
		goods.setGoodsIssale(0);
		goods.setGoodsKeysword(null);

		setMinGoodsPrice(goodskuList, goods);

		tblWxGoodsMapper.insertSelective(goods);

		for (TblWxGoodSku sku : goodskuList) {
			sku.setGoodsSkuStatus(1);
			sku.setGoodsId(goods.getGoodsId());
			tblWxGoodSkuMapper.insertSelective(sku);
		}
	}

	@Override
	@Transactional
	public void updateGoods(AddGoodsVo addGoodsVo) {

		List<TblWxGoodSku> goodskuList = addGoodsVo.getGoodskuList();

		TblWxGoods goods = new TblWxGoods();
		goods.setGoodsId(addGoodsVo.getGoodsId());
		goods.setGoodsCheck(addGoodsVo.getGoodsCheck());
		goods.setGoodsDesc(getImgStr(addGoodsVo.getGoodsDesc()));
		goods.setGoodsGallery(getImgStr(addGoodsVo.getGoodsGallery()));
		goods.setGoodsHot(addGoodsVo.getGoodsHot());
		goods.setGoodsImg(addGoodsVo.getGoodsImg());
		goods.setGoodsWeight(addGoodsVo.getGoodsWeight());
		goods.setGoodsUpdateTime(System.currentTimeMillis());
		goods.setGoodsUnit(addGoodsVo.getGoodsUnit());
		goods.setGoodsTips(addGoodsVo.getGoodsTips());
		goods.setGoodsStock(addGoodsVo.getGoodsStock());
		goods.setGoodsOriginality(addGoodsVo.getGoodsOriginality());
		goods.setGoodsNum(addGoodsVo.getGoodsNum());
		goods.setGoodsName(addGoodsVo.getGoodsName());
		goods.setGoodsIsfreight(addGoodsVo.getGoodsIsfreight());
		goods.setGoodsIsrecom(addGoodsVo.getGoodsIsrecom());
		goods.setGoodsIssale(0);
		setMinGoodsPrice(goodskuList, goods);
		tblWxGoodsMapper.updateByPrimaryKeySelective(goods);

		tblWxGoodSkuMapper.deleteAllSkuByGoodsId(addGoodsVo.getGoodsId());

		if (goodskuList != null && !goodskuList.isEmpty()) {
			for (TblWxGoodSku sku : goodskuList) {
				sku.setGoodsSkuStatus(1);
				sku.setGoodsId(addGoodsVo.getGoodsId());
				tblWxGoodSkuMapper.insertSelective(sku);
			}
		}
	}

	@Override
	@Transactional
	public void updadeGoodsSkuPriceInfo(AddGoodsVo addGoodsVo) {

		List<TblWxGoodSku> goodskuList = addGoodsVo.getGoodskuList();
		TblWxGoods goods = new TblWxGoods();
		goods.setGoodsId(addGoodsVo.getGoodsId());
		setMinGoodsPrice(goodskuList, goods);
		goods.setGoodsUpdateTime(System.currentTimeMillis());
		tblWxGoodsMapper.updateByPrimaryKeySelective(goods);
		
		for (TblWxGoodSku tblWxGoodSku : goodskuList) {
			tblWxGoodSkuMapper.updateByPrimaryKeySelective(tblWxGoodSku);
		}
		
	}

	private String getImgStr(String[] arr) {

		StringBuilder sb = new StringBuilder();
		if (arr != null) {
			for (int i = 0; i < arr.length; i++) {
				if (!ObjectEmptyUtils.isEmptyString(arr[i])) {
					sb.append(arr[i]).append(",");
				}
			}
			return sb.toString();
		}
		return null;
	}

	private String[] getImgArr(String str) {
		String[] strs = { "", "", "", "" };
		if (!ObjectEmptyUtils.isEmptyString(str)) {
			String[] split = str.split(",");
			if (!ObjectEmptyUtils.isEmptyArray(split)) {
				for (int i = 0; i < split.length; i++) {
					strs[i] = split[i];
				}
			}
		}
		return strs;
	}

	private void setMinGoodsPrice(List<TblWxGoodSku> goodskuList, TblWxGoods goods) {
		List<BigDecimal> priceList = new ArrayList<BigDecimal>();
		if (!ObjectEmptyUtils.isEmptyList(goodskuList)) {
			for (TblWxGoodSku sku : goodskuList) {
				priceList.add(sku.getGoodsSkuDisct());
			}
			Collections.sort(priceList);
			for (TblWxGoodSku sku : goodskuList) {
				if (sku.getGoodsSkuDisct().compareTo(priceList.get(0)) == 0) {
					goods.setGoodsMarketprice(sku.getGoodsSkuPrice());
					goods.setGoodsShopprice(sku.getGoodsSkuDisct());
				}
			}
		}
	}
}
